$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'storeProductPrice/storeproductprice/list',
        datatype: "json",
        colModel: [
            {label: 'stepId', name: 'stepId', index: 'step_id', width: 50, key: true},
            {label: '产品id', name: 'productId', index: 'product_id', width: 80},
            {label: '商户id', name: 'sellerId', index: 'seller_id', width: 80},
            {label: '价格', name: 'price', index: 'price', width: 80},
            {label: '阶梯价', name: 'stepPrice', index: 'step_price', width: 80},
            {label: '备注', name: 'marks', index: 'marks', width: 80},
            {label: '更新人', name: 'updateName', index: 'update_name', width: 80},
            {label: '更新时间', name: 'updateTime', index: 'update_time', width: 80},
            {label: '伪删除标记（0正常 1删除）', name: 'delete', index: 'delete', width: 80},
            {label: '创建人', name: 'createName', index: 'create_name', width: 80},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
    //请求后台获得门店分类树
    $.get(baseURL + "storeProductPrice/storeproductprice/storeCategoryTree", function (r) {
        vm.storeCategoryTree = r.storeCategoryTree;
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        storeProductPrice: {},
        storeCategoryTree: null,
        categoryId: null,
        goodsCategoryDialogVisible: false,
        storeGoodsProductEntityList: null,
        priceDialog: false,
        tableIndex: null,
        priceInput: null,
        marksInput: null,
        startPriceInput:null,
        stepPriceList: [],
        stepPriceListView: null,
        queryName:null,
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.storeProductPrice = {};
        },
        update: function (event) {
            var stepId = getSelectedRow();
            if (stepId == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(stepId)
        },
        saveOrUpdate: function (event) {
            var url = vm.storeProductPrice.stepId == null ? "storeProductPrice/storeproductprice/save" : "storeProductPrice/storeproductprice/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.storeProductPrice),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var stepIds = getSelectedRows();
            if (stepIds == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "storeProductPrice/storeproductprice/delete",
                    contentType: "application/json",
                    data: JSON.stringify(stepIds),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (stepId) {
            $.get(baseURL + "storeProductPrice/storeproductprice/info/" + stepId, function (r) {
                vm.storeProductPrice = r.storeProductPrice;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        },
        choiceTree: function () {
            vm.goodsCategoryDialogVisible = true;
        },
        categoryChange: function (value) {
            //取到末级的分类id
            var catId = value[value.length - 1];
            //找该分类下所有的商品
            $.get(baseURL + "storeProductPrice/storeproductprice/getStoreProductByCatId/" + catId, function (r) {
                vm.storeGoodsProductEntityList = r.storeGoodsProductEntityList;
                //关闭分类dialog
                vm.goodsCategoryDialogVisible = false;
            });
        },
        handleEdit: function (index, row) {

            if(row.goodsType == "fast" || row.goodsType == 'print'){
                alert("商务印刷和数码快印门店不用维护价格!")
                return;
            }
            vm.tableIndex = index;
            //加载该阶梯价数据
            $.get(baseURL + "storeProductPrice/storeproductprice/getStoreProductByProductId/" + row.productId, function (r) {
                vm.priceInput = r.resultMap.priceInput;
                vm.marksInput = r.resultMap.marksInput;
                if (!isBlank(r.resultMap.stepPriceList)) {
                    vm.stepPriceList = r.resultMap.stepPriceList;
                }
                if(!isBlank(r.resultMap.startPriceInput)){
                    vm.startPriceInput = r.resultMap.startPriceInput;
                }
                // vm.stepPriceListView = vm.stepPriceList;
                vm.priceDialog = true;
            });
        },
        //将id为priceInput的输入框值,赋值到制定row中
        savePrice: function () {
            if(this.validator()){
                return ;
            }

            vm.storeGoodsProductEntityList[vm.tableIndex].storePrice = vm.priceInput;
            //保存相关价格,生成记录
            this.$confirm('即将生成门店价格信息, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //操作.....
                var StorePriceVo = {};
                StorePriceVo.storeGoodsProductEntity = vm.storeGoodsProductEntityList[vm.tableIndex];
                StorePriceVo.stepPrice = JSON.stringify(vm.stepPriceList);
                StorePriceVo.marks = vm.marksInput;
                StorePriceVo.startPrice = vm.startPriceInput;
                $.ajax({
                    type: "POST",
                    url: baseURL + "storeProductPrice/storeproductprice/createStorePrice",
                    contentType: "application/json",
                    data: JSON.stringify(StorePriceVo),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                //关闭dialog,reload
                                vm.priceDialog = false;
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消生成'
                });
            });
        },
        closePriceDialog: function () {
            vm.priceInput = null;
            vm.tableIndex = null;
            vm.startPriceInput = null;
            //将阶梯价清空
            vm.stepPriceList = [];
        },
        //阶梯价格----------------------------------
        addStepPriceList: function () {
            vm.stepPriceList.push({minSum: null, maxSum: null, price: null})
        },
        search:function(){
            $.get(baseURL + "storeProductPrice/storeproductprice/searchByName/" + vm.queryName, function (r) {
                vm.storeGoodsProductEntityList = r.storeGoodsProductEntityList;
            });

        },
        treeClick: function (data) {
            //找该分类下所有的商品
            $.get(baseURL + "storeProductPrice/storeproductprice/getStoreProductByCatId/" + data.value, function (r) {
                vm.storeGoodsProductEntityList = r.storeGoodsProductEntityList;
            });
        },
        delRow: function (index, stepPriceList) {
            stepPriceList.splice(index, 1);
        },
        loadStepPrice: function (row, event) {
            $.get(baseURL + "storeProductPrice/storeproductprice/getStoreProductByProductId/" + row.productId, function (r) {
                if (JSON.stringify(r.resultMap) != '{}') {
                    vm.stepPriceListView = r.resultMap.stepPriceList;
                }
            });
        },
        validator:function(){
            if(isBlank(vm.priceInput)||vm.priceInput == 0){
                alert("产品价格为空不能保存")
                return true;
            }
            debugger;
            for(var i = 0;i<vm.stepPriceList.length;i++){
                var stepPrice = vm.stepPriceList[i];
                if(isBlank(stepPrice.minSum)){
                    alert("第"+(i+1)+"行阶梯价起始数量为空,请检查!");
                    return true;
                }
                if(isBlank(stepPrice.maxSum)){
                    alert("第"+(i+1)+"行阶梯价结束数量为空,请检查!");
                    return true;
                }
                if(isBlank(stepPrice.price)){
                    alert("第"+(i+1)+"行价格为空,请检查!");
                    return true;
                }
            }
            return false;
        }
    }
});